'use strict';

/**
 * @ngdoc function
 * @name payPcApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the payPcApp
 */
angular.module('payPcApp')
    .controller('bookdetailCtrl', function($scope, booksearch_model, $location, user_model) {
        console.log($location);
        booksearch_model.getdata_isbn({
            isbn: $location.$$search.isbn
        }).success(function(response) {
            console.log(response);
            $scope.book_info = response.list[0];
        })

        $scope.collectbook = function() {
            user_model.collectABook({
                isbn: $location.$$search.isbn
            }).success(function(response) {
                if (response.statusCode == 0) {
                    swal("提示!", "收藏成功!", "success");
                } else if (response.statusCode == 40003) {
                    swal("提示!", "您已收藏本书!", "success");
                } else {
                    swal("提示!", "收藏失败!", "warning");
                }
                console.log(response);
            })
        };
        $scope.loanABook = function() {
            user_model.loanABook({
                isbn: $location.$$search.isbn
            }).success(function(response) {
                if (response.statusCode == 0) {
                    swal("提示!", "预约成功!", "success");
                    $scope.land_finish = true;
                } else if (response.statusCode == 40003) {
                    swal("提示!", "您已预约本书,请不要重复预约!", "warning");
                    $scope.land_finish = true;
                } else {
                    swal("提示!", "预约失败!", "warning");
                }
            })
        };
        $scope.can_lend_number = function(book_info) {
            return _.filter(book_info, function(num) {
                return num.state == 0;
            }).length;
        };
        $scope.have_lend_number = function(book_info) {
            return _.filter(book_info, function(num) {
                return num.state == 2;
            }).length;
        }
    });